<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
	var basic_Url="${ctx}/logic/purchasereturn";
	var grid;
	var url;
	$(function(){
		//单据状态
		var billSt = '${pcsr.zd006Status}';
		
		var column1 = [{ display : 'id', name : 'id', hide : true},
						{ display: '物料id', name: 'zd003MaterId', hide : true },
						{ display: '<font color="gray">物料编码</font>', name: 'zd004MaterCode', width: 120},
		                { display: '<font color="gray">物料名称</font>', name: 'zd005MaterName', width: 180 },
		                { display: '产品型号<font color="red">*</font>', name: 'zd008MaterModel', width: 100 },
	 				  	{ display: '产品尺寸<font color="red">*</font>', name: 'zd012MaterSize', width: 100 },
	 				    { display: '产品颜色<font color="red">*</font>', name: 'zd010MaterColor', width: 80 },
	 				    { display: '产品材质<font color="red">*</font>', name: 'zd011MaterQuality', width: 160 },
		                { display: '单位<font color="red">*</font>', name: 'zd016Unit', width: 100 },
		                { display: '<font color="gray">单价</font>', name: 'zd014Price', width: 100},
		                { display: '<font color="gray">含税单价</font>', name: 'zd013TaxPrice', width: 100},
		                { display: '<font color="gray">订单数量</font>', name: 'zd022Quantity', width: 100},
		                { display: '<font color="gray">入库总数量</font>', name: 'zd018InQuantiy', width: 120},
		                { display: '<font color="gray">已退货数量</font>', name: 'zd024HsBackQuantity', width: 120},
		                { display: '退货数量<font color="red">*</font>', name: 'zd020BackQuantity', width: 120, editor :{type : 'float'}},
		                { display: '<font color="gray">描述</font>', name: 'zd007MaterSpec', width: 245 }
		                ];
		var column2 = [{ display : 'id', name : 'id', hide : true},
						{ display: '物料id', name: 'zd003MaterId', hide : true },
						{ display: '<font color="gray">物料编码</font>', name: 'zd004MaterCode', width: 120},
		                { display: '<font color="gray">物料名称</font>', name: 'zd005MaterName', width: 180 },
		                { display: '产品型号<font color="red">*</font>', name: 'zd008MaterModel', width: 100 },
	 				  	{ display: '产品尺寸<font color="red">*</font>', name: 'zd012MaterSize', width: 100 },
	 				    { display: '产品颜色<font color="red">*</font>', name: 'zd010MaterColor', width: 80 },
	 				    { display: '产品材质<font color="red">*</font>', name: 'zd011MaterQuality', width: 160 },
		                { display: '单位<font color="red">*</font>',    name: 'zd016Unit', width: 100 },
		                { display: '<font color="gray">单价</font>',    name: 'zd014Price', width: 100},
		                { display: '<font color="gray">含税单价</font>', name: 'zd013TaxPrice', width: 100},
		                { display: '<font color="gray">订单数量</font>', name: 'zd022Quantity', width: 100},
		                { display: '<font color="gray">入库总数量</font>', name: 'zd018InQuantiy', width: 120},
		                { display: '<font color="gray">已退货数量</font>', name: 'zd024HsBackQuantity', width: 120},
		                { display: '退货数量<font color="red">*</font>', name: 'zd020BackQuantity', width: 120},
		                { display: '<font color="gray">描述</font>',    name: 'zd007MaterSpec', width: 245 }
		                ];
		var column3 = column1;
		if('PCSR_APPROVE'==billSt){
			column3 = column2;
		}
			grid = $("#table1").ligerGrid({
		        columns: column3,
		        enabledEdit: true,
		        usePager: true,pageSize:1000,pageSizeOptions:[1000],
		        width: "100%", height:300, checkbox: true,
		        parms:[{ name : 'pcsrId' , value : '${pcsr.id}'}],
		        url : basic_Url+"/gridItems.do",
		        toolbar: { items: [{ text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }]}
		    });
		$("#pageloading").hide();
		
		$("#inputForm").validate({			   
			onsubmit: true,
			 /*执行ajaxsubmit  */ 
		    submitHandler: function(editform) {
		        try{			    		
		            url =  basic_Url+"/save.do";
		            var api = frameElement.api;
		        	var W = api.opener;
		            getReturnMessage(url,"inputForm",W.searchAP);
		        }catch(e){alert(e);}
		    }
		});
	
		
		//按钮可见/不可见
		if(billSt=='' || '${flag}'=='ADD'){
			//新增
			$("#forExcel").hide();
			$("#forSubmit").hide();
			$("#forBack").hide();
			$("#forAudit").hide();
			$("#forAuditEnd").hide();
			$("#forApproved").hide();
		}else if(billSt=='PCSR_SAVE'){
			//暂存
			$("#forExcel").hide();
			$("#forSave").hide();
			$("#forBack").hide();
			$("#forAudit").hide();
			$("#forAuditEnd").hide();
			$("#forApproved").hide();
		}else if(billSt=='PCSR_SUBMIT'){
			//已提交待二审
			$("#forExcel").hide();
			$("#forSave").hide();
			$("#forSubmit").hide();
			$("#forApproved").hide();
		}else if(billSt=='PCSR_AUDIT'){
			//已二审待三审
			$("#forExcel").hide();
			$("#forSave").hide();
			$("#forSubmit").hide();
			$("#forAudit").hide();
			$("#forAuditEnd").hide();
		}else if(billSt=='PCSR_APPROVE'){
			//已三审
			$("#forExcel").hide();
			$("#forSave").hide();
			$("#forSubmit").hide();
			$("#forBack").hide();
			$("#forAudit").hide();
			$("#forAuditEnd").hide();
			$("#forApproved").hide();
		}else if(billSt=='PCSR_BACK'){
			//已驳回
			$("#forExcel").hide();
			$("#forSave").hide();
			$("#forBack").hide();
			$("#forAudit").hide();
			$("#forAuditEnd").hide();
			$("#forApproved").hide();
		}else{
			
		}
	});
	//提交方法
	function submitForm(param){
		var data = grid.getData();
		var dl=grid.getDeleted();
		if(data.length==0){
			$.dialog.alert("请选择申请物料");
	   	 	return false;
		}
		var ba=true;
		if(data !=null){
			var i=0;
			$(data).each(function (){
				i++;
				if(this.zd020BackQuantity<=0){
					$.dialog.alert("您第" + i + "行的退货数量不能小于等于0");
					ba = false;
					return false;
				}
				if (isNaN(this.zd020BackQuantity) ||this.zd020BackQuantity==undefined ||this.zd020BackQuantity==null) {
					ba = false;
					$.dialog.alert("您第" + i + "行的退货数量没有填写");
					return false;
				}
				if(this.zd024HsBackQuantity == null || this.zd024HsBackQuantity.length<=0){
					this.zd024HsBackQuantity = 0;
				}
				if((parseFloat(this.zd020BackQuantity)+parseFloat(this.zd024HsBackQuantity))>parseFloat(this.zd018InQuantiy)){
					$.dialog.alert("您第" + i + "的退货数量大于单据的入库数量");
					ba = false;
					return false;
				}
			});
		}
		jQuery.ajaxSettings.async = false;
		if(param==3){
			getDG().dialog({content: "是否审核完结请选择(Yes或者No)!",width: 250,lock: true,max: false,min: false,
				title:'Confirm Infor',
				button: [{name: 'Yes',
					    callback: function(){
						    if (ba){
						    	$("#flagMark").val(param);
					          	$("#itemdata").val(JSON.stringify(data));
					          	$("#itemdele").val(JSON.stringify(dl));
					          	$("#inputForm").submit();
							}
						}},
					    {name: 'No',focus: true}]
			}).zindex();
		}else{
			if (ba){
				$("#flagMark").val(param);
				$("#itemdata").val(JSON.stringify(data));
				$("#itemdele").val(JSON.stringify(dl));
				$("#inputForm").submit();
			}
		}
	}

	function deleteRow() {
		grid.deleteSelectedRow();
	}
	function exportAP(){
		var id=$("#id").val();
		if(id!=""){
			var path = basic_Url+"/exportExcel.do?id="+id;
	   		window.open(path,"_blank");
		}
		else{
			$.dialog.alert("请先保存再导出");
		}
	}

	
	//采购单
function PurchaseReturnWindow() {
	getDG().dialog(
			{
				title : '选择采购单',
				width : 650,
				height : 350,
				content : 'url:' + sys_context + '/public/windowalert/purchase.do', 
				lock : true,
				max : true,
				min : true,
				ok : function() {
					var rows = this.content.getSelected();
					if(rows==null){
						$.dialog.alert("请选择数据");
						return;
					}
					else{
						if(rows.length>1){
							$.dialog.alert('只能选择一行数据!');
							return;
						}else{
							$(rows).each(function() {						
								$("#zd001PcsCode").val(this.zd020Code);
								$("#zd001PcsId").val(this.id);
						  });
						}
					}
					this.content.Close();
					return false;
				},
				cancelVal : '关闭',
				cancel : true
			}).zindex();
	
}

//仓库
function  SotrageinfoWindow() {
	getDG().dialog(
			{
				title : '选择仓库',
				width : 650,
				height : 350,
				content : 'url:' + sys_context + '/public/windowalert/sotrageinfo.do',    
				lock : true,
				max : true,
				min : true,
				ok : function() {
					var rows = this.content.getSelected();
					if(rows==null){
						$.dialog.alert("请选择数据");
						return;
					}
					else{
						if(rows.length>1){
							$.dialog.alert('只能选择一行数据!');
							return;
						}else{
							$(rows).each(function() {
								$("#zd007WhId").val(this.id);
								$("#zd007WhCode").val(this.zd002Code);
								$("#zd007WhName").val(this.zd003Name);
						  });
						}
					}
					this.content.Close();
					return false;
				},
				cancelVal : '关闭',
				cancel : true
			}).zindex();
}
//采购入库单
function  pcsmWindow() {
	var zd001PcsId =$("#zd001PcsId").val();
	if(zd001PcsId==null||zd001PcsId==""){
		$.dialog.alert("请选则采购编号");
		return false;
	}
	getDG().dialog(
			{
				title : '选择采购入库单号',
				width : 650,
				height : 350,
				content : 'url:' + sys_context + '/public/windowalert/pcsm.do?pcsId='+zd001PcsId,    
				lock : true,
				max : true,
				min : true,
				ok : function() {
					var rows = this.content.getSelected();
					if(rows==null){
						alert("请选择数据");
						return;
					}
					else{
						if(rows.length>1){
							alert('只能选择一行数据!');
							return;
						}else{
							$(rows).each(function() {
						    	$("#zd002PcsmId").val(this.id);
								$("#zd002PcsmCode").val(this.zd003Code);
								$("#zd004PiCode").val(this.zd016PiCode);
								$("#zd007WhId").val(this.zd005WhId);
								$("#zd007WhCode").val(this.zd006WhCode);
								$("#zd007WhName").val(this.zd007WhName);
								$("#zd008SpName").val(this.zd008SupeName);
					    		$("#zd008SpId").val(this.zd008SupeId);
								$("#zd008SpCode").val(this.zd008SupeCode);
					        	multipleSearch("#table1",true,"",basic_Url+"/pcsmItemList.do?pcsmId="+this.id);
						  });
						}
					}
					this.content.Close();
					return false;
				},
				cancelVal : '关闭',
				cancel : true
			}).zindex();
	
}
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${pcsr.id}"/>
<input type="hidden" id="bizVersion" name="bizVersion" value="${pcsr.bizVersion}"/>
<input type="hidden" id="zd004PiCode" name="zd004PiCode" value="${pcsr.zd004PiCode }"/>
<input type="hidden" id="itemdata" name="itemdata" value=""/>
<input type="hidden" id="itemdele" name="itemdele" value=""/>
<input type="hidden" id="flagMark" name="flagMark" value=""/>
<!-- 用于标识提交按钮 -->
<input type="hidden" id="submitFlag" value="" />
	<div class="pageContent" >
    	<div class="panel">
        	<table class="TableBlock" align="center">
         		<c:if test="${not empty pcsr.zd003Code }">
	            	<tr>
	                	<td nowrap>退货单据编号:</td>
	   		            <td>
	   		            	<input type="text" id="zd003Code" name="zd003Code"  readonly="readonly" class="required inp_width_98p" value="${pcsr.zd003Code}" />
	   		            </td>
	   		            <td colspan="2"></td>
	   		        </tr>
   		        </c:if>
   		        <tr>
   		        	<c:choose>
   		           		<c:when test="${empty pcsr.zd001PcsCode}">
   		           			<td nowrap>采购编号:</td>
	                    	<td>
	   		                 	<input type="hidden" id="zd001PcsId" name="zd001PcsId" value="${pcsr.zd001PcsId }"/>
	   		                 	<input type="text" style="width:200px;" id="zd001PcsCode"  name="zd001PcsCode" value="${pcsr.zd001PcsCode }"/>
   		        				<input type="button" value="..." onclick="PurchaseReturnWindow();" />
	   		                </td>
   		           		</c:when>
   		           		<c:otherwise>
   		           		<td nowrap>采购编号:</td>
   		           			 <td>
   		           			 	<input type="hidden" id="zd001PcsId"  name="zd001PcsId" value="${pcsr.zd001PcsId }"/>
   		                 		<input type="text" class="inp_width_98p" id="zd001PcsCode" name="zd001PcsCode" readonly="readonly" value="${pcsr.zd001PcsCode}"/>
   		                	 </td>
   		           		</c:otherwise>
   		           	</c:choose>
   		           	<c:choose>
   		           		<c:when test="${empty pcsr.zd002PcsmCode}">
   		           			<td nowrap>采购入库单号:</td>
	                    	<td>
	                    	    <input type="text" id="zd002PcsmCode" name="zd002PcsmCode" value="${pcsr.zd002PcsmCode }"/>
	   		                 	<input type="hidden" style="width:200px;" id="zd002PcsmId"  name="zd002PcsmId"  value="${pcsr.zd002PcsmId }" />
   		        				<input type="button" value="..." onclick="pcsmWindow();" />
	   		                </td>
   		           		</c:when>
	   		           	<c:otherwise>
	   		           		<td nowrap>采购入库单号:</td>
	   		           		<td>
	   		           			<input type="hidden" id="zd002PcsmId"  name="zd002PcsmId"  value="${pcsr.zd002PcsmId }" />
	   		            		<input type="text" class="inp_width_98p" id="zd002PcsmCode" readonly="readonly" name="zd002PcsmCode" value="${pcsr.zd002PcsmCode}"/>
	   		            	</td>
	   		           	</c:otherwise>
   		           	</c:choose>
   		       	</tr>
   		        <tr>
					<td nowrap>供应商:</td>
   		            <td>
   		            	<input type="text" id="zd008SpName" readonly="readonly" name="zd008SpName" class="required inp_width_98p" value="${pcsr.zd008SpName}" />
   		            	<input type="hidden" id="zd008SpId" readonly="readonly" name="zd008SpId"  class="inp_width_98p" value="${pcsr.zd008SpId}"/>
   		                <input type="hidden" id="zd008SpCode" readonly="readonly" name="zd008SpCode"  class="inp_width_98p" value="${pcsr.zd008SpCode}"/>
   		            </td>
   		            <td nowrap>退货日期:</td>
					<td>
   		            	<input type="text" id="zd011BackDate" name="zd011BackDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="required inp_width_98p" value="<fmt:formatDate value="${pcsr.zd011BackDate}" pattern="yyyy-MM-dd" type="both" />"  readonly="readonly"/>
   		                <c:if test="${pcsr.zd011BackDate == null}">
							<script>
								var date = new Date();
								$("#zd011BackDate").val(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
							</script>
						</c:if>
   		           	</td>	
   		        </tr>
   		        <tr>
   		        	<td nowrap>退货仓库:</td>
                    <td>
   		                <input type="hidden" id="zd007WhCode" name="zd007WhCode" value="${pcsr.zd007WhCode}"/>
   		                <input type="text" id="zd007WhName" name="zd007WhName" value="${pcsr.zd007WhName}" readonly="readonly"/>
	   		            <input type="hidden" style="width:200px;" id="zd007WhId" name="zd007WhId" value="${pcsr.zd007WhId}" />
   		        		<!-- <input type="button" value="..." onclick="SotrageinfoWindow();" /> -->
   		           	</td>
   		            <td nowrap>制单人:</td>
   		            <td>
   		            <c:choose>
   		              <c:when test="${not empty pcsr.bizCreatorName}">
   		                 <input type="text" id="bizCreatorName" name="bizCreatorName" class="required inp_width_98p" readonly="readonly" value="${pcsr.bizCreatorName}" />
   		              </c:when>
   		              <c:otherwise>
   		              
   		            	 <input type="text" id="bizCreatorName" name="bizCreatorName" class="required inp_width_98p" readonly="readonly" value="${user.name}" />
   		              </c:otherwise>
   		            </c:choose>
   		           
   		            </td>	
   		        </tr>
   		        <tr>
   		        	<td nowrap>备注:</td>
   		           	<td colspan="3">
   		           		<input id="zd013Remark" name="zd013Remark" class="inp_width_98p" value="${pcsr.zd013Remark}" />
   		           	</td>
   		        </tr>
            </table>
            <div id="table1"> </div>               
       	</div>
		<div class="formBar">
	        <ul>
	        	<li><div class="button"><div class="buttonContent"><button type="button" id="forExcel" class="export" onclick="exportAP()">导出</button></div></div> </li>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forSave" onclick="submitForm('PCSR_SAVE')">保存</button></div></div></li>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forSubmit" onclick="submitForm('PCSR_SUBMIT')">提交</button></div></div></li>
		       <webres:displaycontroller elementId="CGRKTH_BH_CHEECK_PH" description="采购退货申请驳回">
		       		<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forBack" onclick="submitForm('PCSR_BACK')">驳回</button></div></div></li>
		      	</webres:displaycontroller>
		       <webres:displaycontroller elementId="CGRKTH_YJ_CHEECK" description="采购退货二级审核">
		       			<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forAudit" onclick="submitForm('PCSR_AUDIT')">二级审核</button></div></div></li>
		       			<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forAuditEnd" onclick="submitForm('PCSR_APPROVE')">二级审核完结</button></div></div></li>
		      		</webres:displaycontroller>
		      		<webres:displaycontroller elementId="CGRKTH_EJ_CHEECK" description="采购退货三级审核">
		       			<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forApproved" onclick="submitForm('PCSR_APPROVE')">三级审核</button></div></div></li>
		      		</webres:displaycontroller>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>
    </div>
</form>
</body>
</html>
